package com.lgy.controller.camunda;

import com.lgy.service.IDeploymentService;
import com.lgy.utils.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.camunda.bpm.engine.repository.DeploymentWithDefinitions;
import org.camunda.bpm.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/deployment")
public class RepositoryController {
    @Autowired
    private IDeploymentService deploymentService;

    @RequestMapping("/create")
    public ResponseResult create(String resourcesName, MultipartFile file) throws IOException {
        if(StringUtils.isBlank(resourcesName)) {
            return ResponseResult.fail("参数资源名称不能为NULL");
        }
        DeploymentWithDefinitions deployment = (DeploymentWithDefinitions) deploymentService.deployment(resourcesName,file.getInputStream());
        ProcessDefinition processDefinition = deployment.getDeployedProcessDefinitions().get(0);
        String processDefinitionId = processDefinition.getId();
        String processDefinitionKey = processDefinition.getKey();
        Map<String, String> data = new HashMap<>();
        data.put("processDefinitionId",processDefinitionId);
        data.put("processDefinitionKey",processDefinitionKey);
        return ResponseResult.success(data);
    }

    @RequestMapping("/deleteByProcessDefId")
    public ResponseResult deleteByProcessDefId(String processDefinitionId)  {
        if(StringUtils.isBlank(processDefinitionId)) {
            return ResponseResult.fail("参数资源名称不能为NULL");
        }
        deploymentService.deleteProcessDefinition(processDefinitionId);
        return ResponseResult.success();
    }
}
